package pagerbanner.library.com;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AccelerateInterpolator;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * author:吴昶
 * 时间: ${data}
 * 简介：基于 viewpager 的轮播 banner
 */
public class PagerBannerView<T> extends ViewPager {

    private OnBannerListener<T> onBannerListener;
    private BannerAdapter<T> bannerAdapter;
    private OnPageChangeListener onPageChangeListener;
    private List<T> bannerItems;
    private int index;
    private Timer timer;
    private TimerTask timerTask;
    private Handler handler=new Handler();
    private PageTransformer mPageTransformer;

    public PagerBannerView(@NonNull Context context) {
        super(context);
    }

    public PagerBannerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 必须提前设置
     */
    public PagerBannerView setOnBannerListener(OnBannerListener<T> onBannerListener) {
        this.onBannerListener = onBannerListener;
        init();
        return this;
    }

    public void setOnCustomerPageChangeListener(OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    public void setBannerItems(List<T> bannerItems) {
        if(bannerItems!=null && !bannerItems.isEmpty()){
            T first=bannerItems.get(0);
            T last=bannerItems.get(bannerItems.size()-1);
            bannerItems.add(0,last);
            bannerItems.add(first);
        }
        this.bannerItems = bannerItems;
        initData();
    }

    private void init(){
        if (onBannerListener==null){
            throw new RuntimeException("must be set onBannerListener<T> before init(), "
                    + "onBannerListener can not be null");
        }
        bannerAdapter= new BannerAdapter<>(getContext(), onBannerListener);
        this.setAdapter(bannerAdapter);

        try {
            BannerScroller bannerScroller = new BannerScroller(getContext(), new AccelerateInterpolator());
            bannerScroller.setmDuration(200);
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            field.set(this, bannerScroller);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

        setCurrentItem(1,false);
        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(onPageChangeListener!=null){
                    onPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }
            }

            @Override
            public void onPageSelected(final int position) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(position==bannerItems.size()-1){
                            setCurrentItem(1,false);
                        }else if(position==0){
                            setCurrentItem(bannerItems.size()-2,false);
                        }
                    }
                },210);
                if(onPageChangeListener!=null){
                    onPageChangeListener.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(onPageChangeListener!=null){
                    onPageChangeListener.onPageScrollStateChanged(state);
                }
            }
        });
    }

    private void initData(){
        bannerAdapter.upDataItems(bannerItems);
        setCurrentItem(1,false);
    }

    public void start(){
        if(timerTask==null){
            timer=new Timer();
            timerTask=new TimerTask() {
                @Override
                public void run() {
                    handler.post(runnable);
                }
            };
            timer.schedule(timerTask,0,2000);
        }
    }

    public void stop(){
        if(timerTask!=null){
            timer.cancel();
            timerTask.cancel();
            timerTask=null;
            timer=null;
        }
    }

    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            setCurrentItem(getCurrentItem()+1);
        }
    };

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if(hasWindowFocus){
            Log.d("=======","正在执行");
        }else {
            Log.d("=======","进入后台");
        }
    }
}
